<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="head-title2">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class="info">
          <el-tooltip placement="left">
            <div slot="content">
              {{define}}
            </div>
            <div class="btns"></div>
          </el-tooltip>
        </div>
      </div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 机构名称 -->
          <div class="searchItem">
            <span>{{$t('achievements.orgName')}}</span>
            <el-select
              v-model="listQuery.orgId"
              filterable
              :placeholder="$t('rule.choose')"
              size="small"
            >
              <el-option
                v-for="item in mechanism"
                :key="item.id"
                :label="item.orgName"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
          <!-- 业务ID -->
          <div class="searchItem">
            <span>{{$t('achievements.orderId')}}</span>
            <el-input v-model="listQuery.orderId" :placeholder="$t('rule.orderId')" size="small"></el-input>
          </div>
          <!-- 客户姓名 -->
          <div class="searchItem">
            <span>{{$t('achievements.realName')}}</span>
            <el-input v-model="listQuery.realName" :placeholder="$t('rule.realName')" size="small"></el-input>
          </div>
          <!-- 手机号 -->
          <div class="searchItem">
            <span>{{$t('achievements.phoneNo')}}</span>
            <el-input v-model="listQuery.phoneNo" :placeholder="$t('rule.phoneNo')" size="small"></el-input>
          </div>
          <!-- 审批结果 -->
          <div class="searchItem">
            <span>{{$t('achievements.result')}}</span>
            <el-select v-model="listQuery.approvalResult" size="small">
              <el-option
                v-for="item in result"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
          <!-- 审批人 -->
          <div class="searchItem">
            <span>{{$t('achievements.loanUser')}}</span>
            <el-input v-model="listQuery.loanUser" :placeholder="$t('rule.loanUser')" size="small"></el-input>
          </div>
          <!-- 申请时间 -->
          <div class="searchItem searchItem-date">
            <span>{{$t('achievements.crtTime')}}</span>
            <el-date-picker
              size="small"
              v-model="applyDate"
              type="daterange"
              :range-separator="$t('btn.go')"
              :start-placeholder="$t('rule.beginDate')"
              :end-placeholder="$t('rule.endDate')"
              value-format="yyyy-MM-dd"
              @change="dateChangeCrtTime"
            ></el-date-picker>
          </div>
          <!-- 初/复审完成时间 -->
          <div class="searchItem searchItem-date">
            <span>{{$t('achievements.checkDate')}}</span>
            <el-date-picker
              size="small"
              v-model="firstApprDate"
              type="daterange"
              :range-separator="$t('btn.go')"
              :start-placeholder="$t('rule.beginDate')"
              :end-placeholder="$t('rule.endDate')"
              value-format="yyyy-MM-dd"
              @change="dateChangeFirstCompleteDate"
            ></el-date-picker>
          </div>
        </div>
        <div class="menu-title-btn">
          <button type="button" @click="search">{{$t('btn.query')}}</button>
          <button type="button" @click="reset">{{$t('btn.reset')}}</button>
          <button
            type="button"
            :disabled="expDis"
            @click="handleDownload"
            v-if="achievements_btn_export"
          >{{$t('btn.export')}}</button>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('achievements.title2')}}</div>
      <!-- <div class='menu-list'>
            <button type='button' @click='approvalFn'>查看审批详情</button>
      </div>-->
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        size="mini"
      >
        <!-- 业务ID -->
        <el-table-column :label="$t('achievements.orderId')" width="140">
          <template slot-scope="scope">
            <!-- <span class="href" @click="hrefFn(scope.row)">{{scope.row.orderId}}</span> -->
            <span >{{scope.row.orderId}}</span>
          </template>
        </el-table-column>
        <!-- 客户姓名 -->
        <el-table-column align="center" header-align="center"  prop="realName" :label="$t('achievements.realName')" width="180"></el-table-column>
        <!-- 手机号 -->
        <el-table-column align="center" header-align="center" prop="appUserName" :label="$t('achievements.phoneNo')" width="180"></el-table-column>
        <!-- 审批人 -->
        <el-table-column align="center" header-align="center" prop="businessUserName" :label="$t('achievements.loanUser')" width="140"></el-table-column>
        <!-- 产品名称 -->
        <el-table-column align="center" header-align="center" prop="proName" :label="$t('achievements.proName')" width="140"></el-table-column>
        <!-- 申请金额 -->
        <el-table-column align="center" header-align="center" prop="applyAmt" :label="$t('achievements.applyAmt')" width="100"></el-table-column>
        <!-- 申请时间 -->
        <el-table-column align="center" header-align="center" prop="crtTime" :label="$t('achievements.crtTime')" width="200"></el-table-column>
        <!-- 初/复审完成时间 -->
        <el-table-column align="center" header-align="center" prop="checkDate" :label="$t('achievements.checkDate')" width="200"></el-table-column>
        <!-- 批准额度 -->
        <el-table-column align="center" header-align="center" prop="approvedAmt" :label="$t('achievements.approvedAmt')" width="100"></el-table-column>
        <!-- 机构名称 -->
        <el-table-column align="center" header-align="center" prop="orgName" :label="$t('achievements.orgName')" width="180"></el-table-column>
        <!-- 业务渠道 -->
        <el-table-column align="center" header-align="center" prop="appShortName" :label="$t('achievements.appShortName')" width="110"></el-table-column>
        <!-- 审批结果 -->
        <el-table-column align="center" header-align="center" prop="status" :label="$t('achievements.apprResult')" width="110">
          <template slot-scope="scope">
            <span v-if="scope.row.status=== 1">{{$t('approval.audit')}}</span>
            <span v-if="scope.row.status=== 2">{{$t('approval.credit')}}</span>
            <span v-if="scope.row.status=== 3">{{$t('approval.repayments')}}</span>
            <span v-if="scope.row.status=== 4">{{$t('approval.refused')}}</span>
            <span v-if="scope.row.status=== 5">{{$t('approval.clear')}}</span>
          </template>
        </el-table-column>
        <!-- 审批意见 -->
        <el-table-column align="center" header-align="center" 
          prop="checkOpinion"
          :label="$t('achievements.checkOpinion')"
          width="300"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { dataMasking } from 'utils/index'
import {
  page,
  getListOrg,
  pageExport
} from "api/AuditReport/apprAchiCount/index";
export default {
  data() {
    return {
      define: '',
      applyDate: [],
      firstApprDate: [new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 2), new Date()],
      achievements_btn_export: false,
      expDis: true,
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        orderId: "", // 业务ID
        realName: "", // 客户姓名
        phoneNo: "", // 手机号
        approvalResult: 0, // 全部0 通过1拒绝2降额3
        orgId: "", // 机构ID
        beginDate: "", // 申请时间
        endDate: "",
        loanUser: "", // 审批人
        beginDate2: "", //审批时间
        endDate2: ""
      },
      tableData: [],
      mechanism: [
        {
          id: 0,
          orgName: this.$t("rule.choose")
        }
      ],
      result: [
        {
          value: 0,
          label: this.$t("rule.all")
        },
        {
          value: 1,
          label: this.$t("approval.adopt")
        },
        {
          value: 2,
          label: this.$t("approval.refuse")
        },
        // {
        //   value: 3,
        //   label: this.$t('approval.derate')
        // },
        {
          value: 4,
          label: this.$t("approval.audit")
        }
      ]
    };
  },
  created() {
    let obj = {};
    getListOrg(obj).then(response => {
      this.mechanism = this.mechanism.concat(response.data.orgList);
    });
    if (this.apprAchiCountPage.has(this.$route.path)) {
      this.listQuery = this.apprAchiCountPage.get(this.$route.path);
    }
    this.getTime();
    this.getList();
    this.achievements_btn_export = this.elements["achievements:btn_export"];
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'APPROVAL_PERFORMANCE_STATISTICS') {
        this.define = item.dictValue
      }
    })
  },
  computed: {
    ...mapGetters(["elements", "apprAchiCountPage", 'getReportDefine'])
  },
  methods: {
    getTime() {
      this.listQuery.beginDate2 = this.GetDateStr(-2);
      this.listQuery.endDate2 = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      let dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      let y = dd.getFullYear();
      let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    // 申请时间
    dateChangeCrtTime(val) {
      if (val === undefined || val === null || val === '') {
        this.listQuery.beginDate = '';
        this.listQuery.endDate = '';
      } else {
        this.listQuery.beginDate = val[0];
        this.listQuery.endDate = val[1];
      }
    },
    // 初审完成时间
    dateChangeFirstCompleteDate(val) {
      if (val === undefined || val === null || val === '') {
        this.listQuery.beginDate2 = '';
        this.listQuery.endDate2 = '';
      } else {
        this.listQuery.beginDate2 = val[0];
        this.listQuery.endDate2 = val[1];
      }
    },
    hrefFn(row) {
      this.$store.dispatch("setApprAchiCountDetails", row);
      this.$router.push({
        path: "/AuditReport/apprAchiCountDetails",
        name: "审批业绩详情 - Beta"
      });
    },
    //点击导出
    handleDownload() {
      this.listLoading = true;
      pageExport(this.listQuery).then(response => {
        response.data.forEach(item => {
          if (item.realName || item.appUserName) {
            item.realName = dataMasking(item.realName)
            item.appUserName = dataMasking(item.appUserName)
          }
          switch (item.status) {
            case 1:
              item.status = this.$t("approval.audit");
              break;
            case 2:
              item.status = this.$t("approval.credit");
              break;
            case 3:
              item.status = this.$t("approval.repayments");
              break;
            case 4:
              item.status = this.$t("approval.refused");
              break;
            case 5:
              item.status = this.$t("approval.clear");
              break;
          }
        });
        require.ensure([], () => {
          const { export_json_to_excel } = require("@/vendor/Export2Excel");
          const tHeader = [
            this.$t("achievements.orderId"),
            this.$t("achievements.realName"),
            this.$t("achievements.phoneNo"),
            this.$t("achievements.loanUser"),
            this.$t("achievements.proName"),
            this.$t("achievements.applyAmt"),
            this.$t("achievements.crtTime"),
            this.$t("achievements.checkDate"),
            this.$t("achievements.approvedAmt"),
            this.$t("achievements.orgName"),
            this.$t("achievements.appShortName"),
            this.$t("achievements.apprResult"),
            this.$t("achievements.checkOpinion")
          ];
          const filterVal = [
            "orderId",
            "realName",
            "appUserName",
            "businessUserName",
            "proName",
            "applyAmt",
            "crtTime",
            "checkDate",
            "approvedAmt",
            "orgName",
            "appShortName",
            "status",
            "checkOpinion"
          ];
          const list = response.data;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(
            tHeader,
            data,
            this.$t("achievements.achievements")
          );
          this.listLoading = false;
        });
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        // if (j === 'realName' || j === 'appUserName') {
        //   return dataMasking(v[j])
        // }
        return v[j]
      }));
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveApprAchiPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      page(this.listQuery).then(response => {
        response.data.rows.forEach(item => {
          let re = /(?=(?!\b)(\d{3})+$)/g;
          let str = String(item.applyAmt);
          item.applyAmt = str.replace(re, ",");
          let str2 = String(item.approvedAmt);
          item.approvedAmt = str2.replace(re, ",");
        });
        if (response.status === 200) {
          this.expDis = false;
          this.listLoading = false;
          this.tableData = response.data.rows;
          this.total = Number(response.data.total);
        }
      });
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.orgId = 0;
      this.listQuery.orderId = "";
      this.listQuery.realName = "";
      this.listQuery.phoneNo = "";
      this.listQuery.approvalResult = 0;
      this.applyDate = [];
      this.listQuery.checkBeginDate = '';
      this.listQuery.checkEndDate = '';
      this.listQuery.beginDate = '';
      this.listQuery.endDate = '';
      this.firstApprDate = [new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 2), new Date()];
      this.listQuery.beginDate = '';
      this.listQuery.endDate = '';
      this.listQuery.beginDate2 = this.GetDateStr(-2);
      this.listQuery.endDate2 = this.GetDateStr(0);
      this.listQuery.loanUser = "";
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 220px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.href {
  border-bottom: 1px solid #000;
  cursor: pointer;
  &:hover {
    color: #20a0ff;
    border-bottom: 1px solid #20a0ff;
  }
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../assets/images/question-mark.svg");
}
</style>